package com.aaa.controller;

import com.aaa.entity.Student;
import com.aaa.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/listAll")
    public PageInfo<Student> listAll(@RequestParam(value = "pageNumber",defaultValue = "1" ,required = false) Integer pageNumber,
                                     @RequestParam(value = "pageSize",defaultValue = "5" ,required = false) Integer pageSize,String stuname,String status){
        PageInfo<Student> pageInfo = studentService.listAll(pageNumber, pageSize,stuname,status);
        return pageInfo;
    }
//    @GetMapping("/selectStuname")
//    public List<Student> selectStuname(String stuname){
//        List<Student> students = studentService.selectStuname(stuname);
//        return students;
//    }
    @PostMapping("/insertOrUpdate")
    public Integer insertOrUpdate(@RequestBody Student student){

        Integer integer = studentService.insertOrUpdate(student);
        return integer;
    }
    @PostMapping("/batchDelete")
    public Integer batchDelete(@RequestBody List<Student> student){
        Integer integer = studentService.batchDelete(student);
        return integer;
    }
    @GetMapping("/selectRemark")
    public List<Student> selectRemark(@RequestParam("id") Integer id){
        List<Student> students = studentService.selectRemark(id);
        return students;
    }
    @RequestMapping("/login")
    public String login(Student user, HttpServletResponse response){
        Subject subject=SecurityUtils.getSubject();
        //当前用户还没有认证；没有登陆
        if (!subject.isAuthenticated()) {
            //用户名密码的号牌信息  从表单中传递过来的用户名 和 密码
            UsernamePasswordToken token = new UsernamePasswordToken(user.getStuname(),user.getPassword());
            //记住我
            token.setRememberMe(true);
            try {
                //调用登陆方法 将号牌委托给---->安全管理器---->进入认证器，调用Realm获取用户信息进行匹配
                subject.login(token);
               response.sendRedirect("/student_vue.html");
            } catch (UnknownAccountException uae) {//账号不存在
                System.out.println("账号不存在");
            } catch (IncorrectCredentialsException ice) {//密码不匹配
                System.out.println("密码不匹配");
            } catch (LockedAccountException lae) {//账户锁定
                System.out.println("账户锁定");
            }
            catch (AuthenticationException ae) {
                System.out.println("认证异常");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "error";
    }

}
